﻿/******************************************************************************
 * 【本类功能概述】                                 					      *
 *  版权所有（C）2017-20XX，米兔网络                                         *
 *  保留所有权利。                                                            *
 ******************************************************************************
 *  作者 : <hughjk>
 *  创建时间:2018年9月4日 15:30:50
 *  文件描述:目标
 *****************************************************************************/

using System;

namespace BrainSpace
{
    public class Goal//目标
    {
        public bool isdebug { get { return Brain.isdebugLog; } }

        public GoalData m_data;

        public Brain m_Brain;
        public ActionCluster StrCondition;//监听
        public ActionCluster Execute;//执行
        public ActionCluster EndCondition;//监听

        public bool IsUse;
        public Action m_Overs;

        public Goal(Brain brain)
        {
            if (isdebug)
            {
                debug.lin();
            }
            m_Brain = brain;
        }

        public void InitData(GoalData data)
        {
            m_data = data;
        }

        public void Init()
        {
            Execute = new ActionCluster(this, m_data.m_execute, ExecuteOver);
            StrCondition = new ActionCluster(this, m_data.m_condition, ConditionOver);
            EndCondition = new ActionCluster(this, m_data.m_condition, Overs);
            StrCondition.Init();
            EndCondition.Init();
            Execute.Init();
        }

        public void Start()
        {
            IsUse = true;
            StrCondition.Start();
        }

        //打断步
        public void BreakOvers()
        {
            if (IsUse)
            {
                IsUse = false;
                if (Execute != null)
                {
                    Execute.BreakOvers();
                    Execute.Dispose();
                    Execute = null;
                }
                if (StrCondition != null)
                {
                    StrCondition.BreakOvers();
                    StrCondition.Dispose();
                    StrCondition = null;
                }
                if (EndCondition != null)
                {
                    EndCondition.BreakOvers();
                    EndCondition.Dispose();
                    EndCondition = null;
                }
            }
        }

        public void ConditionOver(int exId)
        {
            if (IsUse == false) return;

            Execute.SelectId = StrCondition.SelectId;
            EndCondition.Start();
            Execute.Start();
        }

        public void ExecuteOver(int exId)
        {
        }

        //当前步结束
        public void Overs(int exId)
        {
            debug.log(isdebug, "引导步", m_data.m_id, "结束 下一步"/*, m_data.UpId*/);
            if (IsUse == false) return;
            IsUse = false;
            if (m_Overs != null)
            {
                m_Overs.Invoke();
            }
        }

        public void Dispose()
        {
            Execute.Dispose();
            StrCondition.Dispose();
        }
    }
}